package com.huatai.datadevelop.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huatai.common.api.R;
import com.huatai.common.error.exception.ServiceException;
import com.huatai.datadevelop.entity.JsonDataEntity;
import com.huatai.datadevelop.service.JsonDataService;
import com.huatai.datadevelop.vo.DeleteVO;
import com.huatai.datadevelop.vo.InterfaceNameComplianceVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Author zhangbenting
 * @Description TODO
 * @Date 2023/4/20 16:28
 * @Version 1.0
 */
@Api(value = "静态JSON解析模块",tags = "静态JSON解析模块")
@Slf4j
@RestController
@RequestMapping("/table/jsondata")
public class JsonDataController {

    @Autowired
    private JsonDataService jsonDataService;

    /**
     * 获取英文名称是否合规
     * @param interfaceNameComplianceVO
     * @return
     */
	@ApiOperation("获取英文名称是否合规")
    @PostMapping("getInterfaceNameCompliance")
    public R getInterfaceNameCompliance(@RequestBody InterfaceNameComplianceVO interfaceNameComplianceVO){
        return R.data(this.jsonDataService.getTableNameCompliance(interfaceNameComplianceVO.getInterfaceName()));
    }

    /**
     * Json数据新增或修改
     * @return
     */
	@ApiOperation("Json数据新增或修改")
    @PostMapping("/jsonDataSaveOrUpdate")
    public R interfaceConfigSaveOrUpdate(@RequestBody JsonDataEntity entity){
        return R.data(this.jsonDataService.getJsonBodyData(entity));
    }

    /**
     * 删除或批量删除
     * @return
     */
	@ApiOperation("删除或批量删除")
    @PostMapping("/deleteByIds")
    public R deleteByIds(@RequestBody DeleteVO data){
        return R.data(this.jsonDataService.removeByIds(data.getIds()));
    }

    /**
     * 获取数据列表
     * @param params
     * @return
     */
	@ApiOperation("获取数据列表")
    @PostMapping("list")
    public R list(@RequestBody Map<String, Object> params){
        return R.data(this.jsonDataService.queryPage(params));
    }

    /**
     * 调用数据
     * @param interfaceName
     * @return
     */
	@ApiOperation("调用数据")
    @PostMapping("/asyncRequestApi/{interfaceName}")
    public R asyncRequestApi(@PathVariable("interfaceName") String interfaceName, @RequestBody JSONObject data){
        QueryWrapper<JsonDataEntity> qw = new QueryWrapper<>();
        qw.eq("interface_name",interfaceName);
        JsonDataEntity entity = this.jsonDataService.getOne(qw);
        if(entity==null){
			throw new ServiceException("查无>>>>"+interfaceName+"<<<<此接口，请核查");
        }
        return R.data(this.jsonDataService.asyncRequestApi(entity,data));
    }

    /**
     * 调用数据
     * @param interfaceName
     * @return
     */
	@ApiOperation("调用数据")
    @PostMapping("/asyncStaticJsonApi/{interfaceName}")
    public R asyncStaticJsonApi(@PathVariable("interfaceName") String interfaceName){
        QueryWrapper<JsonDataEntity> qw = new QueryWrapper<>();
        qw.eq("interface_name",interfaceName);
        JsonDataEntity entity = this.jsonDataService.getOne(qw);
        if(entity==null){
            throw new ServiceException("查无>>>>"+interfaceName+"<<<<此接口，请核查");
        }
        return R.data(this.jsonDataService.asyncStaticJsonApi(entity));
    }

}
